;; -*- mode: emacs-lisp; coding: gbk-dos; -*-
;; Daniel Carson <edaniel.shen@gmail.com>

;;(load-file "~/.emacs.d/etc/muse/muse-init.el")

(require 'outline)
(require 'org)
(require 'muse)
(require 'muse-colors)
(require 'muse-mode)
(require 'muse-html)
(require 'muse-wiki)
(require 'muse-journal)

;; (require 'org) is needed
;;(define-key muse-mode-map (kbd "<tab>") 'org-cycle)
;;(define-key muse-mode-map (kbd "<S-iso-lefttab>") 'org-shift-tab)

(define-key global-map "\C-c\C-f" 'muse-project-find-file)

;; (add-hook 'muse-mode-hook
;;           '(lambda ()
;;              (setq outline-regexp "\\*+ ")
;;              (outline-minor-mode)
;;              (hide-body)))

(setq muse-html-meta-content-type (concat "text/html; charset=utf-8"))

;; (setq muse-mode-auto-p nil)
;; (setq muse-mode-highlight-p t)
;; (setq muse-wiki-ignore-bare-project-names t)
;; (setq muse-colors-evaluate-lisp-tags nil)
;; (setq htmlize-convert-nonascii-to-entities nil)

;;(setq muse-colors-autogen-headings 'outline)

;; (setq debug-on-error t)

;; ;; coding
(setq auto-coding-alist
      (append auto-coding-alist '(("\\.muse\\'" . utf-8))))
(setq muse-html-encoding-default 'utf-8)
(setq muse-html-charset-default "utf-8")
(add-hook 'muse-mode-hook #'(lambda ()
                              (set-buffer-file-coding-system 'utf-8)))

(custom-set-variables
 '(muse-html-meta-content-encoding (quote utf-8))
 '(muse-html-charset-default "utf-8")
 '(muse-html-encoding-default (quote utf-8))
 )

(require 'muse-project)

(defun foo-muse-handle-file-link (&optional string)
  (or string
      (setq string
            (let ((end (save-excursion
                         (re-search-forward "]" nil t)
                         (1- (point)))))
              (when end
                (buffer-substring-no-properties (point) end)))))
  (setq string
        (expand-file-name
         (concat (muse-get-keyword :path (nth 2 (muse-project)))
                 "/" string)))
  (when (file-exists-p string)
    string))
(add-to-list 'muse-explicit-link-functions 'foo-muse-handle-file-link)


(defun foo-muse-relative-path (file)
  (concat
   (file-relative-name
    my-muse-publish-directory
    (file-name-directory muse-publishing-current-output-path))
   file))

(defun foo-muse-generate-index (&optional project-list)
  "Generate the index of all wikifile except the file in the\"website\" project"
  (unless project-list
    (setq project-list (list (car (muse-project)))))
  (let (project-files title)
    (muse-with-temp-buffer
      (dolist (project project-list)
        (setq project-files (muse-project-file-alist project))
        (progn
          (insert "* " project "\n"))
        (dolist (file project-files)
          (unless (or (equal (car file) "index")
                      (equal (car file) "WikiIndex"))
            (insert " - [[" project "#" (car file) "]["
                    (save-excursion
                      (set-buffer (generate-new-buffer "*index*"))
                      (insert-file-contents (cdr file))
                      (goto-char (point-min))
                      (setq title
                            (if (re-search-forward "^#title" nil t)
                                (buffer-substring (point) (line-end-position))
                              (car file)))
                      (kill-buffer (current-buffer))
                      title)
                    "]]\n")))
        (insert "\n"))
      (buffer-string))))


(setq shenfn-style-sheet
      (concat "<link rel=\"stylesheet\" type=\"text/css\""
              " charset=\"utf-8\" media=\"all\""
              " href=\"../common/stylesheets/shenfn.css\" />"))

(muse-derive-style "shenfn-xhtml" "xhtml"
                   :header "~/Documents/shenfn.com/common/templates/header.html"
                   :footer "~/Documents/shenfn.com/common/templates/footer.html"
                   :style-sheet shenfn-style-sheet)

(setq muse-project-alist
      `(
        ("Yian"
         ("~/Documents/shenfn.com/yian"
          :default "index")
         (:base "shenfn-xhtml"
                :base-url "http://shenfn.com/yian"
                :path "~/Documents/shenfn.com/yian"))
        ("Emacs"
         ("~/Documents/shenfn.com/Emacs"
          :default "index")
         (:base "shenfn-xhtml"
                :base-url "http://shenfn.com/emacs"
                :path "~/Documents/shenfn.com/emacs"))
        ("About"
         ("~/Documents/shenfn.com/about"
          :default "index")
         (:base "shenfn-xhtml"
                :base-url "http://shenfn.com/about"
                :path "~/Documents/shenfn.com/about"))
         ("Journal"
          ("~/Documents/shenfn.com/journal"
           :default "index")
           (:base "journal-xhtml"
                  :base-url "http://shenfn.com/journal"
                  :path "~/Documents/shenfn.com/journal")
           (:base "journal-rss"
                  :base-url "http://shenfn.com/journal"
                  :path "~/Documents/shenfn.com/journal")
            )
        ("shenfn" (,@(muse-project-alist-dirs "~/Documents/shenfn.com")
                   :default "index"
                   )
         ,@(muse-project-alist-styles "~/Documents/shenfn.com"
                                      "~/Documents/shenfn.com"
                                      "shenfn-xhtml"))
        ("ABM"
         ("~/Documents/SRE/source/abm"
          :default "ABMIndex"
          :force-publish ("WikiIndex"))
         (:base "xhtml"
                :base-url "http://null.com/abm/"
                :path "~/Documents/SRE/publish/abm"))
        ("SRE" (,@(muse-project-alist-dirs "~/Documents/SRE/source")
                :default "Index"
                :force-publish ("WikiIndex")
                )
         ,@(muse-project-alist-styles "~/Documents/SRE/source"
                                      "~/Documents/SRE/publish"
                                      "xhtml"))
        ))

(setq my-muse-publish-directory "~/Documents/shenfn.com/")